﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;

public partial class Admin_Article_ArticleEdit : System.Web.UI.Page
{
    private int articleId;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!int.TryParse(Request.QueryString["articleId"], out articleId))
            return;
        if (!IsPostBack)
            BindData();
    }

    protected void BindData()
    {
        #region 控件数据
        
        MatchIns.BLL.TArticleCategory bll = new MatchIns.BLL.TArticleCategory();
        List<MatchIns.Model.TArticleCategory> ds = new List<MatchIns.Model.TArticleCategory>();
        bll.GetList(0, bll.GetList(), ds, "");

        category.Items.Clear();
        category.DataSource = ds;
        category.DataTextField = "CategoryName";
        category.DataValueField = "ArticleCategoryId";
        category.DataBind();
        category.Items.Insert(0, new ListItem("请选择..."));
        #endregion

        #region 文章数据
        MatchIns.Model.TArticle model = new MatchIns.BLL.TArticle().GetModel(articleId);
        if (model == null)
            return;

        newstitle.Text = model.Title;
        subhead.Text = model.Subhead;
        author.Text = model.Author;
        category.SelectedValue = model.ArticleCategoryId.ToString();
        weight.Text = model.Weight.ToString();
        hd_smallImg.Value = model.SmallImg;
        content.Value = model.Content;
        #endregion

    }
    protected void save_Click(object sender, EventArgs e)
    {
        MatchIns.Model.TArticle model = new MatchIns.Model.TArticle();
        model.ArticleId = articleId;
        model.Title = newstitle.Text.Trim();
        model.Subhead = subhead.Text.Trim();
        model.Author = author.Text.Trim();

        int _categoryid;
        int _sort;
        int.TryParse(category.SelectedValue, out _categoryid);
        int.TryParse(weight.Text, out _sort);

        model.ArticleCategoryId = _categoryid;
        model.Weight = _sort;
        model.Content = content.Value;
        model.UpdateTime = DateTime.Now;
        if (smallImg.HasFile)
        {
            try
            {
                string fileName = MatchIns.Common.Upload.UploadImage(smallImg, "~/UploadFile/Aticle/", 150);
                model.SmallImg = fileName;
            }
            catch (Exception ex)
            {
                errors.Text = ex.Message;
            }

        }
        else
        {
            model.SmallImg = hd_smallImg.Value;
        }
        new MatchIns.BLL.TArticle().Update(model);
        errors.Text = "<script type='text/javascript'>alert('文章修改成功');location.href='ArticleList.aspx';</script>";

    }
    protected void cancel_Click(object sender, EventArgs e)
    {

    }

}
